#include "aichatdialog.h"
#include "apikeysdialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include <QSettings>

AIChatDialog::AIChatDialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("BMI智能问答");
    resize(600, 500);

    // 创建DeepSeek API实例
    deepseekApi = new DeepSeekAPI(this);
    connect(deepseekApi, &DeepSeekAPI::responseReceived, this, &AIChatDialog::displayAnswer);
    connect(deepseekApi, &DeepSeekAPI::errorOccurred, this, &AIChatDialog::displayError);

    // 创建界面元素
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    // 聊天显示区域
    chatDisplay = new QTextEdit(this);
    chatDisplay->setReadOnly(true);
    chatDisplay->setPlaceholderText("在这里将显示AI回答...");
    mainLayout->addWidget(chatDisplay);
    
    // 欢迎信息
    chatDisplay->append(tr("<b>BMI智能助手:</b> 您好！我是您的BMI健康顾问。您可以向我咨询任何关于BMI的问题，例如："));
    chatDisplay->append(tr("• BMI是什么？如何计算？"));
    chatDisplay->append(tr("• 不同BMI值代表什么健康状况？"));
    chatDisplay->append(tr("• 如何健康地改善我的BMI值？"));
    chatDisplay->append(tr("• 针对不同年龄段的BMI标准是什么？"));
    chatDisplay->append(tr("请在下方输入您的问题。"));
    
    // 问题输入区域
    QHBoxLayout *inputLayout = new QHBoxLayout();
    questionInput = new QLineEdit(this);
    questionInput->setPlaceholderText("输入您的BMI相关问题...");
    sendButton = new QPushButton("发送", this);
    settingsButton = new QPushButton("设置", this);
    
    inputLayout->addWidget(questionInput);
    inputLayout->addWidget(sendButton);
    inputLayout->addWidget(settingsButton);
    mainLayout->addLayout(inputLayout);
    
    // 连接信号和槽
    connect(sendButton, &QPushButton::clicked, this, &AIChatDialog::sendQuestion);
    connect(questionInput, &QLineEdit::returnPressed, this, &AIChatDialog::sendQuestion);
    connect(settingsButton, &QPushButton::clicked, this, &AIChatDialog::openApiKeysDialog);
    
    // 检查API密钥是否已设置
    QSettings settings("BMI8", "DeepSeekAPI");
    QString apiKey = settings.value("apiKey").toString();
    QString apiEndpoint = settings.value("apiEndpoint").toString();
    if (apiKey.isEmpty() || apiKey == "YOUR_DEEPSEEK_API_KEY" || apiEndpoint.isEmpty()) {
        QMessageBox::information(this, "设置API", 
                               "请先设置DeepSeek API密钥和端点才能使用AI问答功能。");
        openApiKeysDialog();
    }
}

AIChatDialog::~AIChatDialog()
{
    // 清理资源
}

void AIChatDialog::setInitialPrompt(const QString &prompt)
{
    // 保留欢迎信息，添加传入的提示
    QString currentText = chatDisplay->toHtml();
    if (currentText.contains("BMI智能助手:")) {
        QString suggestion = QStringLiteral(
"<b>当前健康建议:</b> ") + prompt;
        chatDisplay->append(suggestion);
    }
    else {
        chatDisplay->setText(prompt);
    }
}

void AIChatDialog::sendQuestion()
{
    QString question = questionInput->text().trimmed();
    if (question.isEmpty()) {
        return;
    }
    
    // 显示用户问题
    chatDisplay->append("<b>您:</b> " + question);
    
    // 清空输入框
    questionInput->clear();
    
    // 显示等待消息
    chatDisplay->append("<i>AI正在思考中...</i>");
    
    // 发送问题到API
    deepseekApi->askQuestion(question);
}

void AIChatDialog::displayAnswer(const QString &answer)
{
    // 移除等待消息
    QString currentText = chatDisplay->toHtml();
    currentText.replace("<i>AI正在思考中...</i>", "");
    chatDisplay->setHtml(currentText);
    
    // 显示AI回答
    chatDisplay->append("<b>BMI智能助手:</b> " + answer);
}

void AIChatDialog::displayError(const QString &errorMessage)
{
    // 移除等待消息
    QString currentText = chatDisplay->toHtml();
    currentText.replace("<i>AI正在思考中...</i>", "");
    chatDisplay->setHtml(currentText);
    
    // 显示错误信息
    chatDisplay->append("<span style='color:red'><b>错误:</b> " + errorMessage + "</span>");
}

void AIChatDialog::openApiKeysDialog()
{
    ApiKeysDialog dialog(this);
    dialog.exec();
}
